﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;
using Hi_Baidu.Entry.CollectionObject;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Packet.InPacket.TalkInPacket;
using System.Collections;

namespace Hi_Baidu.Notify.NotifyObject
{
    /// <summary>
    ///     提供了提示未看文字消息的任务栏提示基本操作
    /// </summary>
    class Notify_TalkMessage : Notify
    {
        String TalkSession;
        public Notify_TalkMessage(String Key, InPackage inpackage, NotifyType.NOTIFYTYPE NotifyType)
            : base(Key, inpackage, NotifyType, (inpackage as TalkMessageInPackage).GetUsername())
        {
        }

        public Notify_TalkMessage(String Key, String username, NotifyType.NOTIFYTYPE NotifyType, String SendTime, String MessageContent, String TalkSession)
            : base(Key, username, NotifyType, username, SendTime,MessageContent)
        {
            TalkSession = TalkSession;
        } 

        public override void Trigger()
        {
            String ForMessageId = "";
            String FriendName = "";
            if (this.NotifyPackage == null)
            {
                FriendName = this.username;
            }
            else
            {
                FriendName = (this.NotifyPackage as TalkMessageInPackage).GetUsername();
            }
            Win_TalkWindow win_TalkWindow = Globle.popWindowManager.GetMemberByKey(FriendName) as Win_TalkWindow;
            if (win_TalkWindow == null)
            {
                win_TalkWindow = new Win_TalkWindow(FriendName, Globle.friendCollection.GetMemberByKey(FriendName) as Friend);
                Globle.popWindowManager.Add(win_TalkWindow);
                if (this.NotifyPackage != null)
                {
                    ChatViewModel.ReadTalkMessage(new Total_TalkMessagePacket((this.NotifyPackage as TalkMessageInPackage).TalkPackage));
                }
                else
                {
                    ChatViewModel.ReadTalkMessage(this.Content, this.username, this.SendTime, this.username, this.TalkSession);
                }
                Globle.systemNotifyIconManager.GetMember().RemoveAt(0);
                //开始分析剩余提示中是否还存在相同提示，-->存在则直接读取
                List<IMetaData> LIST_NOTIFY = Globle.systemNotifyIconManager.GetMember();
                int Count = LIST_NOTIFY.Count;
                ArrayList AL_Object = new ArrayList();

                for (int i = 0; i < Count; i++)
                {
                    if (LIST_NOTIFY[i] is Notify_TalkMessage)
                    {
                        Notify_TalkMessage notify_TalkMessage = LIST_NOTIFY[i] as Notify_TalkMessage;
                        if (notify_TalkMessage.Tag.ToString() == FriendName)
                        {
                            if (this.NotifyPackage != null)
                            {
                                ChatViewModel.ReadTalkMessage(new Total_TalkMessagePacket((this.NotifyPackage as TalkMessageInPackage).TalkPackage));
                            }
                            else
                            {
                                ChatViewModel.ReadTalkMessage(this.Content, this.username, this.SendTime, this.username,this.TalkSession);
                            }
                            //移除
                            AL_Object.Add(LIST_NOTIFY[i]);
                        }
                    }
                }

                for (int j = 0; j < Count; j++)
                {
                    LIST_NOTIFY.Remove(AL_Object[j] as IMetaData);
                }
                win_TalkWindow.Show();
            }
            else
            {
                ChatViewModel.ReadTalkMessage(new Total_TalkMessagePacket((this.NotifyPackage as TalkMessageInPackage).TalkPackage));
                Globle.popWindowManager.ActiveWindow(FriendName);
            }
        }
    }
}
